package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @author 32338
 * @date 2024-04-12 15:56:48
 * @description 菜品管理
 */
@Slf4j
@RestController
@RequestMapping("/admin/dish")
@Api("菜品管理")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    private void cleanCache(String pattern){
        //清理缓存数据
        log.info("清理redis中缓存的数据：{}",pattern);
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }

    //============================================================

    /**
     * @description
     ***:新增菜品
     **/
    @PostMapping
    @ApiOperation("新增菜品")
    public Result save(@RequestBody DishDTO dishDTO){
        log.info("新增菜品");
        //清理缓存数据
        String key = "dish_"+dishDTO.getCategoryId();
        cleanCache(key);

        dishService.save(dishDTO);
        return Result.success();
    }

    /**
     * @description
     ***:修改菜品
     **/
    @PutMapping
    @ApiOperation("修改菜品")
    public Result update(@RequestBody DishDTO dishDTO){
        log.info("修改菜品：{}",dishDTO);

        //批量删除redis中的菜品
        cleanCache("dish_*");

        dishService.updateWithFlavor(dishDTO);
        return Result.success();
    }

    /**
     * @description
     ***:起售停售菜品
     **/
    @PostMapping("/status/{status}")
    public Result statusOrStop(@PathVariable Integer status,Long id){
        log.info("起售停售菜品");

        //批量删除redis中的菜品
        cleanCache("dish_*");

        dishService.statusOrStop(status,id);
        return Result.success();
    }

    /**
     * @description
     ***:分页查询
     **/
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
        log.info("分页查询条件：{}",dishPageQueryDTO);

        PageResult pageResult = dishService.pageQuiry(dishPageQueryDTO);

        return Result.success(pageResult);
    }

    /**
     * @description
     ***:根据ids批量删除菜品
     **/
    @DeleteMapping
    @ApiOperation("根据ids批量删除菜品")
    public Result deleteByIds(@RequestParam List<Long> ids){
        log.info("根据Ids批量删除菜品：{}",ids);
        //批量删除redis中的菜品
        cleanCache("dish_*");

        dishService.deleteByIds(ids);
        return Result.success();
    }

    /**
     * @description
     ***:根据id来查询菜品和对应的口味数据
     **/
    @GetMapping("/{id}")
    public Result<DishVO> getDishById(@PathVariable Long id){
        log.info("根据id来查询菜品和对应的口味数据:{}",id);
        DishVO dishVO = dishService.getDishWithFlavorById(id);
        return Result.success(dishVO);
    }

    /**
     * @description
     ***:根据分类id查询菜品
     **/
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> list(Long categoryId){
        List<Dish> list = dishService.list(categoryId);
        return Result.success(list);
    }
}
